[XEND] Proper importing of Xen API VM Struct into new XendConfig
authorAlastair Tse <atse@xensource.com>
Fri, 1 Dec 2006 17:30:39 +0000 (17:30 +0000)
committerAlastair Tse <atse@xensource.com>
Fri, 1 Dec 2006 17:30:39 +0000 (17:30 +0000)
Use a smarter method of importing the Xen API VM struct when adding to
XendConfig.

Also add a commented __setitem__ override for XendConfig to type check
certain configuration values.

Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/xen/xend/XendConfig.py

index 698c4a348790d9bddc5d11b8a3445e3293d6ff51..5733abf34c9cffd5cc13e2d7f7cc74f074ed4e41 100644 (file)
@@ -106,7 +106,7 @@ XENAPI_CFG_TYPES = {
     'platform_clock_offset': bool0,
     'platform_enable_audio': bool0,
     'platform_keymap': str,
-    'boot_method': int,
+    'boot_method': str,
     'builder': str,
     'kernel_kernel': str,
     'kernel_initrd': str,
@@ -263,7 +263,7 @@ class XendConfig(dict):
             self._sxp_to_xapi(sxp_obj)
             self._sxp_to_xapi_unsupported(sxp_obj)
         elif xapi:
-            self.update(xapi)
+            self.update_with_xenapi_config(xapi)
             self._add_xapi_unsupported()
         elif dominfo:
             # output from xc.domain_getinfo
@@ -274,6 +274,21 @@ class XendConfig(dict):
         # validators go here
         self.validate()
 
+    """ In time, we should enable this type checking addition. It is great
+        also for tracking bugs and unintended writes to XendDomainInfo.info
+    def __setitem__(self, key, value):
+        type_conv = XENAPI_CFG_TYPES.get(key)
+        if callable(type_conv):
+            try:
+                dict.__setitem__(self, key, type_conv(value))
+            except (ValueError, TypeError):
+                raise XendConfigError("Wrong type for configuration value " +
+                                      "%s. Expected %s" %
+                                      (key, type_conv.__name__))
+        else:
+            dict.__setitem__(self, key, value)
+    """
+
     def _defaults(self):
         defaults = {
             'uuid': uuid.createString(),
@@ -715,6 +730,22 @@ class XendConfig(dict):
         self._dominfo_to_xapi(dominfo)
         self.validate()
 
+    def update_with_xenapi_config(self, xapi):
+        """Update configuration with a Xen API VM struct
+
+        @param xapi: Xen API VM Struct
+        @type xapi: dict
+        """
+        for key, val in xapi.items():
+            key = key.lower()
+            type_conv = XENAPI_CFG_TYPES.get(key)
+            if callable(type_conv):
+                self[key] = type_conv(val)
+            else:
+                self[key] = val
+
+        self.validate()
+
     def to_xml(self):
         """Return an XML string representing the configuration."""
         pass